package cn.edu.ctbu.datajpa01.service.impl;


import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.repository.StudentRepository;
import cn.edu.ctbu.datajpa01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class StudentImplService  implements StudentService {
    @Autowired
    private StudentRepository studentRepository;



    public List<Student> findAll(){

        return  studentRepository.findAll();
    }

    @Override
    public Page<Student> findAll(Pageable pageable) {
        return studentRepository.findAll(pageable);
    }

    /***
     * 分页 带过滤的查询
     *
     */
    public Page<Student> findAll(Example<Student> student, Pageable pageable){
        return studentRepository.findAll(student,pageable);
    }




    /**
     * 按id进行查询
     * @param id 主键，整数
     * @return
     */
    public Student findById(Integer id){


        return studentRepository.findById(id).orElse(null);
    }


    /**
     * 按名字进行模糊查询 like操作
     * @param name
     * @return
     */


    /**
     * 按 名字和密码进行查询 like操作
     * @param name
     * @return
     */
    public List<Student> findByNameAndPassword(String name,String password){

        return studentRepository.findByNameAndPassword(name,password);
    }

    public Student Insert(Student student){
        studentRepository.save(student);

        return student;
    }
    public Student Update(Student student){
        studentRepository.save(student);

        return student;
    }
    public void Delete(Integer id){
        Student student=new Student();
        student.setId(id);
        studentRepository.deleteById(id);

    }
    public  Student getById(Integer id){
        Student student=studentRepository.findById(id).orElse(null);

        return student;
    }
    public  Student getByNo(String no){
        Student student=studentRepository.findByNo(no);

        return student;
    }
    public List<Student> findByName(String name){
        List<Student> students=studentRepository.findByNameLike(name);
        return students;


    }




}
